package com.zcsy.manage.web.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zcsy.commons.model.RequestContext;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.common.CommonService;

/**
 * @Title: UserSessionFilter.java
 * @Package： com.zcsy.shop.web.listener 
 * @Description: 请求拦截器,用于用户拦截
 *
 * @author tanghan barbadosliy@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年7月10日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@WebFilter(urlPatterns = { "/*" })
public class UserSessionFilter implements Filter {
	
	private static final List<String> WHITE_LIST = new ArrayList<String>();

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(final ServletRequest req, final ServletResponse resp, final FilterChain chain) throws IOException,
	ServletException {
		
		final HttpServletRequest request = (HttpServletRequest) req;
		final HttpServletResponse response = (HttpServletResponse) resp;
		
		//保存当前请求和响应
		RequestContext.setRequest((HttpServletRequest) request);
		RequestContext.setResponse((HttpServletResponse) response);

		final User user = CommonService.getSessionUser();
		 String uri = request.getRequestURI();
		 //取带参数的完整uri路径
		 String queryString = request.getQueryString();
		 if(queryString != null){
			 uri = uri + "?" + queryString;
		 }
		final String loginResource = request.getContextPath() + "/";
		
		//登陆方法跳过
		if (loginResource.equals(uri)) {
			chain.doFilter(request, response);
			return;
		}

		if (user == null && !isWhiteUri(uri)) {
			response.setStatus(401);
			response.setHeader("sessionstatus", "timeout");
			response.sendRedirect(loginResource);
			return;
		}
		
//		addCurrentPosition(uri , request);

		chain.doFilter(request, response);
	}

	
	
	
	private boolean isWhiteUri(final String uri) {
		for (final String whiteUrl : WHITE_LIST) {
			if (uri.contains(whiteUrl)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void init(final FilterConfig config) throws ServletException {
		//添加白名单
		WHITE_LIST.add("/images/");
		WHITE_LIST.add("/css/");
		WHITE_LIST.add("/js/");
		WHITE_LIST.add("/login");
		WHITE_LIST.add("/isUser");
		WHITE_LIST.add("/findPassword");
		WHITE_LIST.add("/sendCode");
		WHITE_LIST.add("/completePassword");
		WHITE_LIST.add("/jumpPage");
		WHITE_LIST.add("/index");
		WHITE_LIST.add("/views/index");
		WHITE_LIST.add("/register");
		WHITE_LIST.add("/saveRegister");
		WHITE_LIST.add("/sendRegisterCode");
		WHITE_LIST.add("/checkPhone");
		
		WHITE_LIST.add("/district/getDistrictInfoByParentId");
		WHITE_LIST.add("/ownerHouse/getDistrictInfoByParentId");
		WHITE_LIST.add("/ownerHouse/getCellListByAreaId");
		WHITE_LIST.add("/ownerHouse/getCellRegionByCellId");
		WHITE_LIST.add("/ownerHouse/getCellBuildingByRegionId");
		WHITE_LIST.add("/ownerHouse/getCellUnitByBuildingId");
		WHITE_LIST.add("/ownerHouse/getCellHouseByUnitId");
		WHITE_LIST.add("/nextStep");
		WHITE_LIST.add("/cellList");
		WHITE_LIST.add("/gotoOpenShop");
		WHITE_LIST.add("/saveLisence");
		WHITE_LIST.add("/fileHandler/uploadCutImage");
		WHITE_LIST.add("/fileHandler/uploadLogoImage");
		WHITE_LIST.add("/tmp");
		WHITE_LIST.add("/gotoSeller");
		
		
		
	}
	
}
